package com.ninetaildemonfox.zdl.socialproject.utils.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kongzue.dialog.v3.WaitDialog;
import com.ninetaildemonfox.zdl.socialproject.base.BaseActivity;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.EditorialMaterialsContract;
import com.ninetaildemonfox.zdl.socialproject.mvp.presenter.EditoriaMaterialsPresenter;
import com.ninetaildemonfox.zdl.socialproject.utils.dialog.adapter.BottomAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author NineTailDemonFox
 * @date 2019/7/18 11:45
 * 功能描述： 底部弹框
 * 联系方式：1037438704@qq.com
 */

public class BottomDialog extends BaseActivity implements EditorialMaterialsContract {

    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.text_parent)
    TextView textParent;
    @BindView(R.id.text_title)
    TextView textTitle;
    @BindView(R.id.recycerView)
    RecyclerView recyclerView;
    @BindView(R.id.fl_context)
    FrameLayout flContext;
    @BindView(R.id.ll_gone_lanjie)
    LinearLayout ll_gone_lanjie;

    private String type;
    private List<Map<String, String>> list;
    private BottomAdapter bottomAdapter;
    private EditoriaMaterialsPresenter editoriaMaterialsPresenter;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_bottom_dialog;
    }

    @Override
    protected void onInitialization(Bundle bundle) {
        editoriaMaterialsPresenter = new EditoriaMaterialsPresenter(this, this);
        initView();
        initData();
    }

    private void initData() {
        bottomAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<Map<String, String>> allData = bottomAdapter.getData();
                String count = allData.get(position).get("count");
                if (count.equals("1")) {
                    allData.get(position).put("count", "2");
                }
                if (count.equals("2")) {
                    allData.get(position).put("count", "1");
                }
                bottomAdapter.notifyDataSetChanged();
                if (type.equals("3")) {
                    String name = allData.get(position).get("name");
                    Bundle bundle = new Bundle();
                    Intent intent = new Intent();
                    bundle.putString("name", name);
                    bundle.putString("appointment_am", position + 1 + "");
                    intent.putExtras(bundle);
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });
    }

    private void initView() {
        list = new ArrayList<>();
        type = getIntent().getStringExtra("count");
        if (type.equals("1")) {
            textTitle.setText("期望对象");
            editoriaMaterialsPresenter.getIndex();
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            bottomAdapter = new BottomAdapter(R.layout.list_bottom);
        } else if (type.equals("2")) {
            textTitle.setText("她的真实评价");
            recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
            bottomAdapter = new BottomAdapter(R.layout.list_bottom_h);
        } else if (type.equals("3")) {
            textTitle.setText("选择时间");
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            bottomAdapter = new BottomAdapter(R.layout.list_dialog_time);
            textCenter.setVisibility(View.GONE);
            textParent.setVisibility(View.GONE);
            time();
            bottomAdapter.setNewData(list);
        } else if (type.equals("4")) {
            textTitle.setText("我的评价");
            recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
            bottomAdapter = new BottomAdapter(R.layout.list_bottom_h);
        }


        bottomAdapter.setType(Integer.valueOf(type));
        recyclerView.setAdapter(bottomAdapter);
    }

    private void time() {
        Map<String, String> map1 = new ArrayMap<>();
        map1.put("count", "1");
        map1.put("name", "上午");
        list.add(map1);

        Map<String, String> map2 = new ArrayMap<>();
        map2.put("count", "1");
        map2.put("name", "中午");
        list.add(map2);

        Map<String, String> map3 = new ArrayMap<>();
        map3.put("count", "1");
        map3.put("name", "下午");
        list.add(map3);


        Map<String, String> map4 = new ArrayMap<>();
        map4.put("count", "1");
        map4.put("name", "晚上");
        list.add(map4);

        Map<String, String> map5 = new ArrayMap<>();
        map5.put("count", "1");
        map5.put("name", "通宵");
        list.add(map5);

        Map<String, String> map6 = new ArrayMap<>();
        map6.put("count", "1");
        map6.put("name", "一整天");
        list.add(map6);
    }

    @OnClick({R.id.text_center, R.id.text_parent, R.id.fl_context, R.id.ll_gone_lanjie})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text_center:
                finish();
                break;
            case R.id.text_parent:
                String str = "";
                String str2 = "";
                List<Map<String, String>> data = bottomAdapter.getData();
                if (data == null) {
                    return;
                }
                for (int i = 0; i < data.size(); i++) {
                    if (data.get(i).get("count").equals("2")) {
                        str = str + data.get(i).get("id") + ",";
                        str2 = str2 + data.get(i).get("name") + ",";
                    }
                }
                if (TextUtils.isEmpty(str)) {
                    return;
                }
                str = str.substring(0, str.length() - 1);
                str2 = str2.substring(0, str2.length() - 1);
                Bundle bundle = new Bundle();
                Intent intent = new Intent();
                bundle.putString("id", str);
                bundle.putString("name", str2);
                intent.putExtras(bundle);
                setResult(RESULT_OK, intent);
                finish();
                break;
            case R.id.fl_context:
                finish();
                break;
            case R.id.ll_gone_lanjie:
                break;
            default:
        }
    }

    public static void start(Context me, String count) {
        Intent intent = new Intent(me, BottomDialog.class);
        intent.putExtra("count", count);
        me.startActivity(intent);
    }

    public static void start(Activity me, String count, int tag) {
        Intent intent = new Intent(me, BottomDialog.class);
        intent.putExtra("count", count);
        me.startActivityForResult(intent, tag);
    }


    @Override
    public void loginSuccess(List<Map<String, String>> data) {
        for (int i = 0; i < data.size(); i++) {
            data.get(i).put("count", "1");
        }
        bottomAdapter.setNewData(data);
    }

    @Override
    public void loginFailed(String msg) {

    }

    @Override
    public void showLoading() {
        WaitDialog.show(me, "数据加载中");
    }
}
